package com.hongxinguoji.manager.content.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hongxinguoji.manager.common.upload.FileUploadService;
import com.hongxinguoji.manager.common.util.EasyUIDataGridResult;
import com.hongxinguoji.manager.common.util.HXResult;
import com.hongxinguoji.manager.content.pojo.HxContent;
import com.hongxinguoji.manager.content.service.IContentService;

@Controller
@RequestMapping("/content")
public class ContentController {

	@Autowired
	private IContentService contentService;

	@RequestMapping("/contentList")
	@ResponseBody
	public EasyUIDataGridResult getContentList(Integer page, Integer rows, Long categoryId) {

		// 查询数据
		List<HxContent> list = contentService.getContentList(categoryId);

		EasyUIDataGridResult result = new EasyUIDataGridResult();

		// 分页处理
		PageHelper.startPage(page, rows);

		PageInfo<HxContent> pageInfo = new PageInfo<>(list);

		result.setTotal(pageInfo.getTotal());
		result.setRows(list);

		return result;
	}

	@RequestMapping("/contentSaveOrUpdate")
	@ResponseBody
	public HXResult addContent(HxContent content) {

		// 如果id值不为空，则执行修改操作
		if (content.getId() != null) {
			try {
				contentService.updateContent(content);
				return HXResult.ok("内容修改成功！");

			} catch (Exception e) {
				e.printStackTrace();
				return HXResult.ok("内容修改失败！");
			}
		} else {

			try {
				contentService.saveContent(content);
				return HXResult.ok("内容发布成功！");

			} catch (Exception e) {
				e.printStackTrace();
				return HXResult.ok("内容发布失败！");
			}

		}

	}

	@RequestMapping("/lock")
	@ResponseBody
	public HXResult lockContent(String ids) {
		// 截取字符串
		String[] idStrings = ids.split(",");

		try {
			// 遍历ID，进行逻辑删除操作
			for (String id : idStrings) {
				String tID = id.trim();
				// 转换id类型
				Long i = Long.parseLong(tID);

				contentService.lockContent(i);
			}
			return HXResult.ok("内容锁定成功！");

		} catch (Exception e) {
			e.printStackTrace();
			return HXResult.ok("内容锁定失败！");
		}

	}

	@RequestMapping("/unlock")
	@ResponseBody
	public HXResult unlockContent(String ids) {
		// 截取字符串
		String[] idStrings = ids.split(",");

		try {
			// 遍历ID，进行逻辑删除操作
			for (String id : idStrings) {
				String tID = id.trim();
				// 转换id类型
				Long i = Long.parseLong(tID);

				contentService.unlockContent(i);
			}
			return HXResult.ok("内容还原成功！");

		} catch (Exception e) {
			e.printStackTrace();
			return HXResult.ok("内容还原失败！");
		}

	}

	@RequestMapping("/delete")
	@ResponseBody
	public HXResult deleteContent(String ids) {
		// 截取字符串
		String[] idStrings = ids.split(",");

		try {
			// 遍历ID，进行逻辑删除操作
			for (String id : idStrings) {
				String tID = id.trim();
				// 转换id类型
				Long i = Long.parseLong(tID);

				contentService.deleteContent(i);
			}
			return HXResult.ok("内容删除成功！");

		} catch (Exception e) {
			e.printStackTrace();
			return HXResult.ok("内容删除失败！");
		}

	}

	/*
	 * banner内容相关页面跳转
	 */
	@RequestMapping("/{path}")
	public String goPath(@PathVariable String path) {
		return "content/" + path;
	}

	/**
	 * 文件上传功能
	 */
	@Autowired
	private FileUploadService fileUploadService;

	@RequestMapping("/file/upload")
	@ResponseBody
	public Map fileUpload(MultipartFile uploadFile) {
		Map result = fileUploadService.uploadFile(uploadFile);
		return result;
	}

}
